package org.ilovecode.display {
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	
	import gs.TweenLite;
	import gs.easing.*;
	
	import org.ilovecode.events.ApartmentEvent;
	import org.ilovecode.events.MenuEvent;
	import org.ilovecode.screen.ScreenGroup;
	import org.ilovecode.screen.ScreenName;
	
	/**
	 * ...
	 * @author Vjekoslav Ratkajec
	 */
	public class FloorLayout extends FloorLayoutBase {
		
		//{ region Stage elements
		
		/*public var _baloonForSale:FloorLayoutBaseBaloon;
		public var _baloonSold:FloorLayoutBaseBaloon;
		public var _baloonReserved:FloorLayoutBaseBaloon;*/
		public var floorNameTextField:TextField;
		public var backButton:SimpleButton;
		public var previousButton:SimpleButton;
		public var nextButton:SimpleButton;
		public var downloadPDFButton:SimpleButton;
		public var progressBar:MovieClip;
		
		//} endregion
		
		private var activeFloor:Floor = null;
		private var activeFloorNumber:int = -1;
		private var numFloorsLoaded:uint = 0;
		private var floorTexts:Array = new Array(
			"TLOCRT PRIZEMLJA",
			"TLOCRT PRVOG KATA",
			"TLOCRT DRUGOG KATA",
			"TLOCRT TREĆEG KATA",
			"TLOCRT ČETVRTOG KATA",
			"TLOCRT PETOG KATA",
			"TLOCRT ŠESTOG KATA",
			"TLOCRT SEDMOG KATA",
			"TLOCRT OSMOG KATA");
		
		private const NUM_FLOORS:uint = 8;
		
		public function FloorLayout() {
			super(ScreenName.FLOOR_LAYOUT, ScreenGroup.APARTMENTS, "floorLayoutApartments");
		}
		
		override protected function initUI():void
		{
			super.initUI();
			
			progressBar.gotoAndPlay(0);
			if (activeFloor != null) activeFloor.visible = false;
			for (var i:uint = 0; i <= NUM_FLOORS; i++) {
				Floor(this["floor" + i]).init(i);
				Floor(this["floor" + i]).mouseEnabled = false;
			}
		}
		
		override protected function addListeners():void
		{
			super.addListeners();
			
			backButton.addEventListener(MouseEvent.CLICK, handle_Click);
			previousButton.addEventListener(MouseEvent.CLICK, handle_Click);
			nextButton.addEventListener(MouseEvent.CLICK, handle_Click);
			downloadPDFButton.addEventListener(MouseEvent.CLICK, handle_Click);
			
			for (var i:uint = 0; i <= NUM_FLOORS; i++) {
				Floor(this["floor" + i]).addEventListener(Event.COMPLETE, floorDataLoadComplete);
				Floor(this["floor" + i]).addEventListener(ApartmentEvent.CLICK, roomClick);
				Floor(this["floor" + i]).addEventListener(ApartmentEvent.MOUSE_OVER, roomMouseOver);
				Floor(this["floor" + i]).addEventListener(ApartmentEvent.MOUSE_OUT, roomMouseOut);
			}
		}
		
		override protected function removeListeners():void
		{
			super.removeListeners();
			
			backButton.removeEventListener(MouseEvent.CLICK, handle_Click);
			previousButton.removeEventListener(MouseEvent.CLICK, handle_Click);
			nextButton.removeEventListener(MouseEvent.CLICK, handle_Click);
			downloadPDFButton.removeEventListener(MouseEvent.CLICK, handle_Click);
			
			for (var i:uint = 0; i <= NUM_FLOORS; i++) {
				Floor(this["floor" + i]).removeEventListener(Event.COMPLETE, floorDataLoadComplete);
				Floor(this["floor" + i]).removeEventListener(ApartmentEvent.CLICK, roomClick);
				Floor(this["floor" + i]).removeEventListener(ApartmentEvent.MOUSE_OVER, roomMouseOver);
				Floor(this["floor" + i]).removeEventListener(ApartmentEvent.MOUSE_OUT, roomMouseOut);
			}
		}
		
		private function handle_Click(event:Event):void
		{
			switch(event.target)
			{
				case backButton:
					//this.dispatchEvent(new MenuEvent(MenuEvent.PREVIOUS, ""));
					controller.showScreen(ScreenName.RENDER);
					break;
				case previousButton:
					//showFloor(activeFloorNumber - 1);
					controller.showFloor(activeFloorNumber - 1);
					break;
				case nextButton:
					//showFloor(activeFloorNumber + 1);
					controller.showFloor(activeFloorNumber + 1);
					break;
				case downloadPDFButton:
					var url:String = "http://www.centar-bundek.hr/Content/PDF/Etaze/Stambena-Etaza-" + activeFloorNumber + ".pdf";
					navigateToURL(new URLRequest(url), "_blank");
					break;
				default:
					break;
			}
		}
		
		private function floorDataLoadComplete(event:Event):void {
			event.currentTarget.removeEventListener(Event.COMPLETE, floorDataLoadComplete);
			numFloorsLoaded++;
			if (numFloorsLoaded >= 9) {
				progressBar.visible = false;
				activeFloor.visible = true;
			}
		}
		
		override protected function roomMouseOver(event:ApartmentEvent):void {
			var room:FloorLayoutBaseRoom = activeFloor.getRoomByName(String(event.item));
			if (room != null) {
				TweenLite.to(room.instance, duration, { alpha:0.6 } );
				toggleBaloon(room, true);
			}
		}
		
		override protected function roomMouseOut(event:ApartmentEvent):void {
			var room:FloorLayoutBaseRoom = activeFloor.getRoomByName(String(event.item));
			if (room != null) {
				TweenLite.to(room.instance, duration, { alpha:0 } );
				toggleBaloon(room, false);
			}
		}
		
		override protected function roomClick(event:ApartmentEvent):void 
		{
			var room:FloorLayoutBaseRoom = activeFloor.getRoomByName(String(event.item));
			if (room != null) {
				var o:Object = new Object();
				o.roomID = room.id;
				o.floorID = activeFloorNumber;
				//this.dispatchEvent(new ApartmentEvent(ApartmentEvent.SELECTED, o));
				controller.showApartment(int(room.id), activeFloorNumber, "floorLayout");
			}
		}
		
		override public function showFloor(floorNumber:int):void {
			for (var i:uint = 0; i <= 8; i++) {
				if (i == floorNumber) {
					activeFloorNumber = floorNumber;
					Floor(this["floor" + i]).visible = true;
					activeFloor = Floor(this["floor" + i]);
					floorNameTextField.text = String(floorTexts[i]);
					
					if (floorNumber == 0)  previousButton.visible = false;
					else previousButton.visible = true;
					if (floorNumber == 8)  nextButton.visible = false;
					else nextButton.visible = true;
					
				} else {
					Floor(this["floor" + i]).visible = false;
				}
			}
		}
		
	}

}